﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Services;
using AmkeniCrossLoginApplication.Data_Access;

namespace AmkeniCrossLoginApplication.Amkeni_Monitoring_Plan.Classes
{
    /// <summary>
    /// Summary description for AutoComplete
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class AutoComplete : System.Web.Services.WebService
    {
        [WebMethod]
        public string[] AutoCompleteAjaxRequest(string prefixText, int count)
        {
            List<string> ajaxDataCollection = new List<string>();
            DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase(prefixText);

            if (_objdt.Rows.Count > 0)
            {
                for (int i = 0; i < _objdt.Rows.Count; i++)
                {
                    ajaxDataCollection.Add(_objdt.Rows[i]["name"].ToString());
                }
            }

            return ajaxDataCollection.ToArray();
        }

        /// <summary>
        /// Function for retriving data from database
        /// </summary>
        /// <returns></returns>
        private DataTable GetDataFromDataBase(string prefixText)
        {
            Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();
            DataTable _objdt = new DataTable();
            //_objdt = (DataTable)db.process_indicator.Where(data => data.name.Contains(prefixText));

            var dataQuery = db.indicators.Where(data => data.indicator_type_id.Equals(2) && data.name.Contains(prefixText));
            _objdt = LINQToDataTable(dataQuery);
            /*foreach (var row in query)
            {
                _objdt.LoadDataRow(new object[] { row.name }, true);
            }*/

            return _objdt;
        }

        /// <summary>
        /// Function for converting LINQ Query into a DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
        {
            DataTable dtReturn = new DataTable();

            //Column Names
            PropertyInfo[] oProps = null;

            if (varlist == null)
            {
                return dtReturn;
            }

            foreach (T rec in varlist)
            {
                //Use reflection to get property names, to create table, only first time, others will follow
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                DataRow dr = dtReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
                }
                dtReturn.Rows.Add(dr);
            }
            return dtReturn;
        }
    }
}
